package com.example.isoftservice.controller;

import com.example.isoftservice.pojos.Category;
import com.example.isoftservice.service.CategoryService;
import com.example.isoftservice.util.Result;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Generated;
import javax.annotation.Resource;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;


    @RequestMapping(value = "/searchCategoryList",method = RequestMethod.GET)
    public Result<Map<String,Object>> searchCategoryList(@RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize) {
        Result<Map<String, Object>> result = new Result<>();
        result.setCode(0);
        result.setResult(categoryService.searchCategoryList(pageNo, pageSize));
        result.success("查询成功");
        result.setSuccess(true);
        return result;
    }


    @RequestMapping(value = "/searchCategoryById",method = RequestMethod.GET)
    public Result<Category> searchCategoryById(Integer categoryId) {
        Result<Category> result = new Result<>();
        Category category = categoryService.searchCategoryById(categoryId);
        if(category != null) {
            result.setCode(0);
            result.setResult(category);
            result.setMessage("查询成功");
            result.setSuccess(true);
            return result;
        } else {
            result.setCode(1);
            result.setMessage("数据不存在");
            return result;
        }

    }


    @RequestMapping(value = "/addCategory",method = RequestMethod.POST)
    public Result<Boolean> addCategory(@RequestBody Category category) {
        Result<Boolean> result = new Result<>();
        result.setCode(0);
        boolean flag = categoryService.addCategory(category);
        result.setResult(flag);
        result.setMessage("增加成功");
        result.setSuccess(flag);
        return result;
    }

    @RequestMapping(value = "/removeCategoryByIds",method = RequestMethod.POST)
    public Result<Boolean> removeCategoryByIds(@RequestBody Category category) {
        Result<Boolean> result = new Result<>();
        result.setCode(0);
        boolean flag = categoryService.removeCategoryByIds(category.ids);
        result.setResult(flag);
        result.setMessage("删除成功");
        result.setSuccess(flag);
        return result;
    }


    @RequestMapping(value = "/modifyCategory",method = RequestMethod.POST)
    public Result<Boolean> modifyCategory(@RequestBody Category category) {
        Result<Boolean> result = new Result<>();
        if(categoryService.searchCategoryById(category.getCategoryId()) == null) {
            result.setMessage("该数据不存在");
            result.setCode(1);
            return result;
        }
        result.setCode(0);
        boolean flag = categoryService.modifyCategoryById(category);
        result.setResult(flag);
        result.setMessage("更新成功");
        result.setSuccess(flag);
        return result;
    }
}
